"""
系统配置文件
"""
import os
from pathlib import Path
from typing import Optional

# 项目根目录
BASE_DIR = Path(__file__).parent.parent.parent

# 数据目录
DATA_DIR = BASE_DIR / "data"
FILES_DIR = DATA_DIR / "files"
MODELS_DIR = DATA_DIR / "models"
TEMP_DIR = DATA_DIR / "temp"

# 确保目录存在
for dir_path in [DATA_DIR, FILES_DIR, MODELS_DIR, TEMP_DIR]:
    dir_path.mkdir(parents=True, exist_ok=True)

# 数据库配置
MYSQL_CONFIG = {
    "host": os.getenv("MYSQL_HOST", "117.72.102.95"),
    "port": int(os.getenv("MYSQL_PORT", "16033")),
    "user": os.getenv("MYSQL_USER", "test"),
    "password": os.getenv("MYSQL_PASSWORD", "StrongPassword123!"),
    "database": os.getenv("MYSQL_DATABASE", "vector_system"),
    "charset": "utf8mb4"
}

# Milvus配置
MILVUS_CONFIG = {
    "host": os.getenv("MILVUS_HOST", "192.168.224.255"),
    "port": os.getenv("MILVUS_PORT", "19530"),
    "user": os.getenv("MILVUS_USER", ""),
    "password": os.getenv("MILVUS_PASSWORD", ""),
    "db_name": os.getenv("MILVUS_DB", "default")
}

# 向量模型配置
VECTOR_MODEL_CONFIG = {
    "model_name": os.getenv("VECTOR_MODEL", "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"),
    "model_path": os.getenv("VECTOR_MODEL_PATH", str(MODELS_DIR / "sentence-transformers")),
    "device": os.getenv("DEVICE", "cpu"),  # cpu, cuda, mps
    "max_length": int(os.getenv("MAX_LENGTH", "512")),  # 最大文本长度
    "batch_size": int(os.getenv("BATCH_SIZE", "32")),   # 批处理大小
    "use_openai_api": os.getenv("USE_OPENAI_API", "true").lower() == "true",  # 是否使用OpenAI API
    "openai_api_base": os.getenv("OPENAI_API_BASE", "http://192.168.224.255:9988/v1"),  # OpenAI API地址
    "openai_api_key": os.getenv("OPENAI_API_KEY", "sk-test"),  # OpenAI API密钥
    "openai_model_name": os.getenv("OPENAI_MODEL_NAME", "qwen3-embedding")  # OpenAI模型名称
}

# Rerank模型配置
RERANK_MODEL_CONFIG = {
    "model_name": os.getenv("RERANK_MODEL", "BAAI/bge-reranker-v2-m3"),
    "model_path": os.getenv("RERANK_MODEL_PATH", str(MODELS_DIR / "rerank")),
    "device": os.getenv("DEVICE", "cpu")
}

# 文件处理配置
FILE_CONFIG = {
    "max_file_size": 100 * 1024 * 1024,  # 100MB
    "allowed_extensions": {
        ".pdf", ".docx", ".doc", ".txt", ".csv",
        ".xlsx", ".xls", ".pptx", ".ppt", ".png",
        ".jpg", ".jpeg", ".gif", ".bmp"
    },
    "chunk_size": 500,  # 文本分块大小
    "chunk_overlap": 200,  # 分块重叠大小
    "max_chunks_per_file": 1000  # 每个文件最大分块数
}

# 向量库配置
VECTOR_STORE_CONFIG = {
    "collection_name": "documents",
    "dimension": 1024,  # 修改为1024以匹配qwen3-embedding模型
    "index_type": "HNSW",  # 索引类型
    "metric_type": "COSINE",  # 距离度量
    "nlist": 1024,  # IVF索引的聚类数
    "m": 16,  # HNSW的M参数
    "efConstruction": 200,  # HNSW构建时的搜索深度
    "ef": 64  # HNSW搜索时的搜索深度
}

# 检索配置
RETRIEVAL_CONFIG = {
    "top_k": int(os.getenv("TOP_K", "20")),
    "similarity_threshold": float(os.getenv("SIMILARITY_THRESHOLD", "-1.0")),  # 降低阈值以便更容易匹配
    "enable_rerank": os.getenv("ENABLE_RERANK", "false").lower() == "true",
    "rerank_top_k": int(os.getenv("RERANK_TOP_K", "10")),
    "enable_hybrid_search": os.getenv("ENABLE_HYBRID_SEARCH", "false").lower() == "true"
}

# Web服务配置
WEB_CONFIG = {
    "host": os.getenv("HOST", "0.0.0.0"),
    "port": int(os.getenv("PORT", "8000")),
    "debug": os.getenv("DEBUG", "False").lower() == "true",
    "reload": os.getenv("RELOAD", "True").lower() == "true",
    "workers": int(os.getenv("WORKERS", "1"))
}

# 日志配置
LOG_CONFIG = {
    "level": os.getenv("LOG_LEVEL", "DEBUG"),  # 改为DEBUG级别以便获取更多日志
    "format": os.getenv("LOG_FORMAT", "%(asctime)s - %(name)s - %(levelname)s - %(message)s"),
    "file": os.getenv("LOG_FILE", "vector_system.log")
}

# 缓存配置
CACHE_CONFIG = {
    "redis_host": os.getenv("REDIS_HOST", "117.72.102.95"),
    "redis_port": int(os.getenv("REDIS_PORT", "19736")),
    "redis_password": os.getenv("REDIS_PASSWORD", "Aresenyang@1217"),
    "redis_db": int(os.getenv("REDIS_DB", "2")),
    "enable_cache": os.getenv("ENABLE_CACHE", "True").lower() == "true"
}